package com.service.schedule.tm;

import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.common.DateUtils;
import com.common.StringUtils;
import com.common.http.HttpResult;
import com.common.model.Global;
import com.dao.tm.PlanDao;
import com.enums.TmPlanStatusEnum;
import com.model.external.sap.SapPlanGoodModel;
import com.model.tm.PlanModel;
import com.service.external.sap.CallSapRfcService;
import com.service.tm.StoreService;

@Service("SapScheduleService")
public class SapScheduleService {

	static final Logger logger = LoggerFactory.getLogger(SapScheduleService.class);

	@Autowired
	CallSapRfcService callSapRfcService;
	@Autowired
	PlanDao planDao;
	@Autowired
	StoreService storeService;

	// 获取实时库存
	public void getStore() {
		String stoDate = DateUtils.getCurrentDate();
		storeService.getStoreFromSap(stoDate);
	}

	// 获取交货单信息
	public void getGodNum() {
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("godNumSap", Global.NO);
		hashMap.put("status", TmPlanStatusEnum.one.getValue());
		hashMap.put("startTime", DateUtils.addDays(DateUtils.getCurrentDate(), -10));
		List<PlanModel> planModels = planDao.selectList(hashMap);
		HttpResult goodHttpResult = callSapRfcService.getPlan(planModels);
		String encodeString = goodHttpResult.getData().toString().replaceAll("\\\\", "");
		List<SapPlanGoodModel> receiveList = JSONArray.parseArray(JSON.toJSON(encodeString).toString(),
				SapPlanGoodModel.class);
		for (SapPlanGoodModel sapPlanGoodModel : receiveList) {
			if (StringUtils.isNullOrEmpty(sapPlanGoodModel.getGOD_CODE())) {
				continue;
			}
			PlanModel model = new PlanModel();
			model.setPlanCode(sapPlanGoodModel.getPLAN_CODE());
			model.setGodNum(sapPlanGoodModel.getGOD_CODE());
			model.setPlanSendRemark(sapPlanGoodModel.getGOD_REMARK());
			switch (sapPlanGoodModel.getSTATUS()) {
			case "C":
				model.setStatus(TmPlanStatusEnum.six.getValue());
				break;
			default:
				model.setStatus(TmPlanStatusEnum.five.getValue());
				break;
			}
			planDao.update(model);
		}
	}

	// 获取交货单最终状态
	public void getGodNumStatus() {
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("status", TmPlanStatusEnum.five.getValue());
		hashMap.put("startTime", DateUtils.addDays(DateUtils.getCurrentDate(), -10));
		List<PlanModel> planModels = planDao.selectList(hashMap);
		HttpResult goodHttpResult = callSapRfcService.getPlan(planModels);
		String encodeString = goodHttpResult.getData().toString().replaceAll("\\\\", "");
		List<SapPlanGoodModel> receiveList = JSONObject.parseArray(encodeString, SapPlanGoodModel.class);
		for (SapPlanGoodModel sapPlanGoodModel : receiveList) {
			if (sapPlanGoodModel.getGOD_CODE().equals(Global.NO + "")) {
				continue;
			}
			switch (sapPlanGoodModel.getSTATUS()) {
			case "C":
				HashMap<String, Object> codeHashMap = new HashMap<String, Object>();
				codeHashMap.put("planCode", sapPlanGoodModel.getPLAN_CODE());
				PlanModel model = planDao.selectList(codeHashMap).get(0);
				model.setPlanSendRemark(sapPlanGoodModel.getGOD_REMARK());
				model.setStatus(TmPlanStatusEnum.six.getValue());
				if (model.getTransportCode().equals(Global.NO + "")) {
					model.setTransportCode(Global.YES + "");
				}
				planDao.update(model);
				break;
			default:
//				model.setStatus(TmPlanStatusEnum.five.getValue());
				break;
			}
		}
	}

}
